interface ListItem {
  isClicked?: boolean;
  title?: string;
  [key: string]: any;
}

export default function StringUtil() {

  const isNull = (value: any): boolean => {
    return !value && typeof value !== "undefined" && value !== 0;
  };

  const isUndefined = (value: any): boolean => {
    return typeof value === 'undefined';
  };

  const isEmpty = (value: any): boolean => {
    return isNull(value) || isUndefined(value);
  };

  const isBlank = (value: any): boolean => {
    return isEmpty(value) || value === '';
  };

  /* 数组转字符串 */
  const listToString = (list: ListItem[], key: string): string => {
    let tempList: any[] = [];
    for (let i = 0; i < list.length; i++) {
      const element = list[i];
      if (element.isClicked) {
        tempList.push(element[key]);
      }
    }
    return tempList.join(',');
  };

  /* 列表对象的isClicked */
  const setListObjectIsClick = (list: ListItem[], obj: string): ListItem[] => {
    let tempList: ListItem[] = list;
    if (isBlank(obj)) {
      return tempList;
    }
    let tempOArr: string[] = obj.split(',');
    for (let i = 0; i < list.length; i++) {
      const element = list[i];
      for (let m = 0; m < tempOArr.length; m++) {
        if (element.title === tempOArr[m]) {
          element.isClicked = true;
          break;
        }
      }
    }
    return tempList;
  };

  return {
    isNull,
    isUndefined,
    isEmpty,
    isBlank,
    listToString,
    setListObjectIsClick
  };
}
